package com.android.juliomedina.parser;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import com.android.juliomedina.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 
 * @author globussoft
 */
public class SingleMenuItemActivity extends Activity {

	static final String KEY_COST = "cost";
	static final String KEY_DESC = "description";
	static final String KEY_NAME = "name";
	static final String KEY_URL = "url";
	static final String KEY_YOUTUBE = "youtube";
	static final String KEY_IMAGE = "image";
	public String youtube = "";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.single_list_item);

		// getting intent data
		Intent in = getIntent();

		// Get XML values from previous intent
		String name = in.getStringExtra(KEY_NAME);
		String cost = in.getStringExtra(KEY_COST);
		String description = in.getStringExtra(KEY_DESC);
		youtube = in.getStringExtra(KEY_YOUTUBE);
		String images = in.getStringExtra(KEY_IMAGE);

		// Displaying all values on the screen
		TextView lblName = (TextView) findViewById(R.id.name_label);
		TextView lblCost = (TextView) findViewById(R.id.cost_label);
		TextView lblDesc = (TextView) findViewById(R.id.description_label);
		ImageView image = (ImageView) findViewById(R.id.image);
		Button bt = (Button) findViewById(R.id.button1);

		lblName.setText(name);
		lblCost.setText(cost);
		lblDesc.setText(description);

		try {

			String url = images;
			URL feedImage = new URL(url);
			HttpURLConnection conn = (HttpURLConnection) feedImage
					.openConnection();
			InputStream is = conn.getInputStream();
			Bitmap img = BitmapFactory.decodeStream(is);
			image.setImageBitmap(img);

		} catch (Exception e2) {
			e2.printStackTrace();
		}

		bt.setOnClickListener(new OnClickListener() {

			@SuppressLint("NewApi")
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (youtube.isEmpty()) {

					Toast.makeText(SingleMenuItemActivity.this,
							"Oops!!! Video Not Available...", Toast.LENGTH_LONG)
							.show();
				} else {
					String s1 = youtube;
					Uri uriUrl = Uri.parse(s1);
					youtube = "";
					Intent launchBrowser = new Intent(Intent.ACTION_VIEW,
							uriUrl);
					startActivity(launchBrowser);

				}

			}

		});

	}

}
